var prefix = jsctx + "/discount";

var discountPolicy = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listDiscountPolicy",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            elecStationId: $("#elecStationId").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },

                        {field: 'policyName', title: '优惠方案名称', width:'100',align: 'left'},
                        {
                            field: 'discountType', title: '优惠类型',
                            width:'100',align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-success">服务费打折</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-info">特定服务费(活动价)</span>'
                                } else if (value == '2') {
                                    return '<span class="label label-warning">总价打折</span>'
                                }
                            }
                        },
                        {
                            field: '', title: '优惠时间', width:'100',align: 'left',
                            width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                return '<span >' + row.beginTime + ' -- ' + row.endTime + ' </span>'
                            }
                        },
                        {
                            field: 'policyMemberType', title: '优惠用户', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >全部</span>'
                                } else if (value == '1') {
                                    return '<span >指定用户</span>'
                                }
                            }
                        },
                        {
                            field: 'status', title: '状态', width:'100',align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >未开始</span>'
                                } else if (value == '1') {
                                    return '<span >进行中</span>'
                                } else if (value == '2') {
                                    return '<span >已结束</span>'
                                } else if (value == '3') {
                                    return '<span >已禁止</span>'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            width:'200',align: 'center',
                            formatter: function (value, row, index) {


                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="详情" onclick="discountPolicy.detailDiscountPolicy(\''
                                    + row.discountPolicyId
                                    + '\')">详情</a> ';

                                var b = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="编辑" onclick="discountPolicy.editDiscountPolicy(\''
                                    + row.discountPolicyId
                                    + '\')">编辑</a> ';

                                if (row.status != '3') {
                                    var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="禁止" onclick="discountPolicy.updateStatus(\''
                                        + row.discountPolicyId
                                        + '\')">禁止</a> ';
                                } else if (row.status == '3') {
                                    var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="已禁止" onclick="">已禁止</a> ';
                                }

                                if (row.status != '3') {
                                    return a + b + c;
                                } else {
                                    return a + c;
                                }

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addDiscountPolicy(elecStationId) {
        isGetOnPolicy(elecStationId);
    }

    function isGetOnPolicy(elecStationId) {
        $.ajax({
            url: prefix + "/isUsingPolicy",
            type: "get",
            data: {
                'elecStationId': elecStationId
            },
            success: function (data) {
                if (data.status == 200) {
                    location.href = prefix + "/addDiscountPolicy/" + elecStationId;
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }

    function editDiscountPolicy(discountPolicyId) {
        location.href = prefix + "/editDiscountPolicy/" + discountPolicyId;
    }

    function detailDiscountPolicy(discountPolicyId) {
        location.href = prefix + "/detailDiscountPolicy/" + discountPolicyId;
    }


    function goBack() {
        window.history.back();
    }

    function updateStatus(discountPolicyId) {
        layer.confirm('确定要禁止方案(不可恢复)？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateStatus",
                type: "post",
                data: {
                    'discountPolicyId': discountPolicyId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        updateStatus: updateStatus,
        detailDiscountPolicy: detailDiscountPolicy,
        addDiscountPolicy: addDiscountPolicy,
        editDiscountPolicy: editDiscountPolicy,
        isGetOnPolicy: isGetOnPolicy
    };
})();

var addDiscountPolicy = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    function save() {
        $("#roleIds").val(getCheckedRoles());
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/saveOrUpdateBanner",
            data: $('#addBanner').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    window.history.back();
                } else {
                    parent.layer.alert(data.message)
                }

            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#addBanner").validate({
            rules: {
                pileNo: {
                    required: true
                },
                pileName: {
                    required: true
                }

            },
            messages: {
                pileNo: {
                    required: icon + "请输入桩群编号"
                },
                pileName: {
                    required: icon + "请输入桩群名称"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
